package com.gree.portal.service.impl;

import com.gree.commom.result.BaseResult;
import com.gree.commom.utils.Md5Util;
import com.gree.commom.utils.RandomUtil;
import com.gree.portal.mapper.AdminMapper;
import com.gree.portal.service.UserService;
import com.gree.sso.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 用户注册
     * @param admin
     * @return
     */
    @Override
    public BaseResult saveUser(Admin admin) throws Exception {
        //生成salt盐并存入用户信息
        String salt = RandomUtil.getRandom1();
        admin.setEcSalt(salt);
        //根据salt加密免密
        String password = Md5Util.getMd5WithSalt(admin.getPassword(), salt);
        admin.setPassword(password);
        //设置注册的时间
        admin.setAddTime((int) LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        int result = adminMapper.insert(admin);
        return result>0?BaseResult.success():BaseResult.error();
    }
}
